package service

import (
	"context"
	"github.com/google/uuid"
	"video-srv/basic/config"
	__ "video-srv/basic/proto/rpc"
	"video-srv/handler/model"
	"video-srv/pkg"
)

func (s *VideoServer) PayMember(_ context.Context, in *__.PayMemberReq) (*__.PayMemberResp, error) {
	tx := config.DB.Begin()
	var user model.User
	err := user.IsUser(tx, int(in.UserId))
	if err != nil {
		tx.Rollback()
		return nil, err
	}
	var member model.Member
	err = member.IsMember(tx, int(in.MemberId))
	if err != nil {
		tx.Rollback()
		return nil, err
	}
	order := model.Order{
		OrderSn:          uuid.NewString(),
		UserId:           int(in.UserId),
		MemberId:         int(in.MemberId),
		PayType:          1,
		Price:            19,
		MemberExpireTime: 900,
	}
	err = order.CreateOrder(tx)
	if err != nil {
		tx.Rollback()
		return nil, err
	}
	url, err := pkg.AliPay(order.OrderSn, order.Price)
	if err != nil {
		tx.Rollback()
		return nil, err
	}
	tx.Commit()
	return &__.PayMemberResp{
		PayUrl: url,
	}, nil
}
